<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 添加参数 -->
      <!-- 搜索添加 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" clearable v-model="queryinfo.query" @clear="getOrdersList">
            <el-button slot="append" icon="el-icon-search" @click="getOrdersList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="ordersList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="100px"></el-table-column>
        <el-table-column prop="pay_status" label="支付状态" width="100px">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'">未支付</el-tag>
            <el-tag type="success" v-else>已支付</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="100px"></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">{{scope.row.create_time | dataFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="编辑订单地址"
              placement="top"
              :enterable="false"
            >
              <el-button type="primary" icon="el-icon-edit" @click="bianjidingdan"></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="物流信息"
              placement="top"
              :enterable="false"
            >
              <el-button type="success" icon="el-icon-location" @click="wuliuxinxi"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 底部分页区域 -->
      <el-pagination
        class="page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[5,10,20,25]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 编辑订单地址 -->
    <el-dialog title="修改地址" :visible.sync="editDialog" @close="editFormclear">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            :options="citydata"
            v-model="editForm.address1"
            :props="cascaderProps"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editForm.address2"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialog = false">取 消</el-button>
        <el-button type="primary" @click="edityujiaoyna">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流信息时间线 -->
    <el-dialog title="物流信息" :visible.sync="progressVisible">
      <el-timeline :reverse="true">
        <el-timeline-item
          v-for="(activity, index) in progressList"
          :key="index"
          :timestamp="activity.time"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import citydata from './citydata.js'
export default {
  data() {
    return {
      citydata,
      editDialog: false,
      progressVisible: false,
      ordersList: [],
      progressList: [],
      total: 0,
      queryinfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      editForm: {
        address1: [],
        address2: '',
      },
      editFormRules: {
        address1: [
          { required: true, message: '请输入省市区/县', trigger: 'blur' },
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
        ],
      },
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'value',
        label: 'label',
        children: 'children',
      },
    }
  },
  created() {
    this.getOrdersList()
  },
  methods: {
    async getOrdersList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryinfo,
      })
      console.log('res',res)
      if (res.meta.status !== 200)
        return this.$message.error('获取订单列表失败')
      this.ordersList = res.data.goods
      // console.log(this.ordersList)
      this.total = res.data.total
      // console.log('query',this.queryinfo);
    },
    handleSizeChange(val) {
      this.queryinfo.pagesize = val
      this.getOrdersList()
    },
    handleCurrentChange(val) {
      this.queryinfo.pagenum = val
      this.getOrdersList()
    },
    // 打开编辑订单对话框
    bianjidingdan() {
      this.editDialog = true
    },
    // 编辑订单
    edityujiaoyna() {
      this.editDialog = false
    },
    // 清理订单表单
    editFormclear() {
      this.$refs.editFormRef.resetFields()
    },

    handleChange() {
      // console.log(this.editForm.address1)
    },
    // 物流信息
    async wuliuxinxi() {
      const { data: res } = await this.$http.get(`/kuaidi/804909574412544580`)
      // console.log(res)
      if (res.meta.status !== 200)
        return this.$message.error('获取物流信息失败')
      this.progressList = res.data
      this.progressVisible = true
    },
  },
}
</script>
<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
